<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Position;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\News\NewsClass;

class PositionController extends Controller
{
    protected $name = '内容管理';
    use ModelForm;
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Position::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            
            $grid->filter(function($filter){
                
                $filter->like('name', '名称');
				$filter->equal('type', '类型')->select(Position::$type);
            
            });
			$grid->actions(function ($actions) {
				$id = $actions->getKey();
				$actions->append('<a href="/admin/positionlist?position_id='.$id.'"><i class="fa fa-eye" title="内容管理"></i></a>');
                $actions->disableView();
			});
            $grid->column('id', 'ID')->sortable();
            
            $grid->column('name', '名称');
            $grid->column('number', '数量');
            
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        return Admin::form(Position::class, function (Form $form) {
            $form->number('id', 'ID')->attribute('disabled', false);
			$form->select('type','位置')->options(Position::$type)->rules('required');
            $form->text('name', '名称')->rules('required|min:1');
            $form->text('remark', '备注');
            $form->number('number', '数量')->default(0)->help('0代表不限制');
			$form->select('pattern','调取类型')->options(Position::$pattern)->attribute('onchange', 'radio_click(this)');
			$form->divide();
            $form->embeds('hand_setting', '手动设置', function ($form) {
				$form->radio('is_two','一行显示多标题')->options(Position::$yesorno)->default('0');
			})->attribute('id', 'Position');
			$form->divide();
			$form->embeds('auto_setting', '自动设置', function ($form) {
				$form->select('order','排序')->options(Position::$selectOrder)->default('1');
				$form->select('class_id','栏目')->options(NewsClass::selectOptions())->help('不选择，则默认不限制')->default(0);
				$form->radio('titlepic','必须有缩略图')->options(Position::$yesorno)->default('0');
				$form->select('time_range','发布时间范围')->options(Position::$selectTime)->help('不选择，则默认不限制')->default(0);
				
			});
			
			$form->hasMany('fields', '字段', function (Form\NestedForm $form) {
				$form->text('name', '中文');
				$form->text('field', '英文');
			});
			$form->html('（默认存在字段title,thumb,url,desc）', '');
			

            
        });
    }
}
